home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / misc / dr.str / PG-HC11 1.1.asm < prev    next >
Assembly Source File  |  1992-10-05  |  18KB  |  593 lines

  1. ************************************************************************
  2. * This file has been converted to compile using the Motorola freeware
  3. * cross-assembler.
  4. *
  5. * Jim Speth   10/5/92
  6. * speth@cats.ucsc.edu
  7. *
  8. ************************************************************************
  9.  
  10. *
  11. * Here's version 1.1 of the 68HC11EVB power glove code. The only change
  12. * is that a port of Dave Stampe's hysterisis deglitching code has been
  13. * added. The routines are activated by sending a "+" and deactivated by
  14. * a "-". 
  15. * Please let me know your results...
  16. *
  17. *    -Ron Menelli       12/10/92
  18. *
  19.  
  20. */**********************************************************************
  21. *  Originally "power.c" (c) manfredo 9/91 (manfredo@opal.cs.tu-berlin.de)
  22. *  Developed on an ATARI 1040ST with TC 1.1 using a logic analyzer to get
  23. *  the correct timings.
  24. ***********************************************************************/
  25. */*********************************************************************
  26. *             ported to PC compatibles by
  27. *                Greg Alt 10/91
  28. *               galt@peruvian.utah.edu
  29. *            or galt@es.dsd.com
  30. ***********************************************************************/
  31. */*********************************************************************
  32. *
  33. * Substantially rewritten by Dave Stampe (c) 1991: PWRFILT.C
  34. *  No cash, no warranty, no flames.
  35. *  This stuff works great, so gimme credit.
  36. *
  37. * Goals <achieved> were:
  38. *
  39. *  Higher speed, smaller code.
  40. *  Polled operation is now possible.
  41. *  Graphics test (VGA)
  42. *  Noise reduction added, gets rid of 99.5% of noise with NO DELAY!
  43. *
  44. *  This runs on a 486/25 with an i/o card. 
  45. *  Someone should adapt it for the usual printer port adapter.
  46. *  It was compiled with Turbo C++ 2.0 but will probably
  47. *  work on any Turbo C directly. MSC will need library calls checked.
  48. *
  49. *
  50. * dstamp@watserv1.uwaterloo.ca          17/10/91
  51. ***********************************************************************/
  52. *
  53. *
  54. * 68HC11 version by Ron Menelli, 10/23/91
  55. *
  56. * A million thanks to the above people for taking this project as far
  57. * as it has gone! This version runs on the MC68HC11 processor, specifically
  58. * Motorola's MC68HC11EVB board. The assembler I used was Matt Dillon's
  59. * DASM for the Amiga - this code will have to be converted a bit to be
  60. * used on Motorola's freeware assembler.
  61. *
  62. * As it stands, this code is a direct port of Dave Stampe's code minus
  63. * the IBM specific stuff (VGA, for example). The way this code works is
  64. * by sending the data received from the Power Glove over the serial port
  65. * at 9600 baud. By sending single character commands, the serial port
  66. * action can be controlled as follows:
  67. *
  68. *       Send    Action
  69. *       ====    =========
  70. *        C      Start continuous mode - send every time the glove is read.
  71. *               The data is sent with a certain byte preceding it as a
  72. *               flag marking the beginning. The format I have used is:
  73. *
  74. *                   A0 X Y Z rot fingers keys
  75. *
  76. *                   ^
  77. *                   +--- Flag character (A0 used for old time's sake!)
  78. *
  79. *        R      Start request mode - send the 6 byte data packet when
  80. *               requested by user. Format is the same as above, minus
  81. *               the flag character.
  82. *
  83. *        ?      In request mode, this causes the controller to report
  84. *               the current glove data.
  85. *
  86. *        +      Turns on hysterisis deglitching mode (new for 1.1)
  87. *
  88. *        -      Turns off hysterisis deglitching mode (new for 1.1)
  89. *
  90. * Please send me any suggestions you have regarding improvements to this
  91. * code!
  92. *
  93. * -Ron Menelli  menelli@tellabs.com
  94. *
  95.  
  96. *************************************************************
  97. **** Must be set depending on type of controller board! *****
  98. *************************************************************
  99. * (If your using the homebrew board, you must ALSO change the
  100. *   last couple of lines in the program.)
  101. *************************************************************
  102. *
  103. * Address of beginning of program:
  104. *
  105. *      $D000 : beginning of RAM for EVB ($D800 is common also)
  106. *      $B600 : beginning of RAM for EVBU
  107. *      $F800 : for homebrew version
  108. *
  109.  
  110.     ORG         $D800           ; Jump to this address to start
  111.  
  112. *
  113. * RAM allocation
  114. *
  115.  
  116. BITCNT      EQU     $0000
  117. BYTECNT     EQU     $0001
  118. GLOVEFLAG   EQU     $0002       ; Flag byte for continuous mode
  119. GLOVEDATA   EQU     $0003       ; 7 byte array
  120. UNREADY     EQU     $0009
  121. MODEFLAG    EQU     $000A       ; Mode flag - continuous or request
  122. OX          EQU     $000B       ; The following are for hysterisis calculations
  123. OY          EQU     $000D
  124. TMP         EQU     $000F
  125. DEGLFLAG    EQU     $0011       ; Deglitching mode flag
  126.  
  127. *
  128. * Port A definitions
  129. *
  130. * bit 0 - Data in
  131. * bit 4 - Clock out
  132. * bit 5 - Latch out
  133. *
  134.  
  135. PORTA       EQU     $1000
  136. PACTL       EQU     $1026
  137. GDATA       EQU     $01
  138. GCLOCK      EQU     $10
  139. GLATCH      EQU     $20
  140. GCLOLAT     EQU     $30
  141.  
  142. *
  143. * Serial port definitions
  144. *
  145.  
  146. BAUD        EQU     $102B
  147. SCCR1       EQU     $102C
  148. SCCR2       EQU     $102D
  149. SCSR        EQU     $102E
  150. SCDR        EQU     $102F
  151.  
  152. *
  153. * Timing constants (for an 8Mhz crystal)
  154. *
  155.  
  156. D2BYTES     EQU     30          ; 96us
  157. D2SLOW      EQU     700         ; ~= 2100us
  158.  
  159. *
  160. * Variables for hysterisis
  161. *
  162.  
  163. XHYST       EQU     2
  164. YHYST       EQU     2
  165.  
  166. *
  167. *Variables for deglitching
  168. *
  169.  
  170. XACC        EQU     8
  171. YACC        EQU     8
  172. XXTEND      EQU     2
  173. YXTEND      EQU     1
  174.  
  175. * Other stuff
  176.  
  177. CONTMODE    EQU     0           ; Continuous mode
  178. REQMODE     EQU     1           ; Request mode
  179. CONTCHAR    EQU     'C          ; Character to request cont. mode
  180. REQCHAR     EQU     'R          ; Character to request request mode
  181. QUERYCHAR   EQU     '?          ; Character to request a data packet
  182. *                                     Only valid in request mode
  183. DEGLCHAR    EQU     '+          ; Character to request deglitching mode
  184. NDGLCHAR    EQU     '-          ; Character to turn off deglitching mode
  185. FLAGCHAR    EQU     $A0         ; Flag indicating beginning of packet in continuous mode
  186.  
  187. *
  188. * ***********************
  189. * * Program begins here *
  190. * ***********************
  191. *
  192.  
  193. INIT:       LDS     #$00FF      ; Initialize stack pointer
  194.  
  195.             LDAA    #0          ; Disable pulse accumulator on port A
  196.             STAA    PACTL
  197.  
  198.             LDAA    #$01        ; Set EVB to serial receive normally
  199.             STAA    $4000
  200.  
  201.             LDAA    #$30        ; Set serial port for 9600 baud, (using 8 MHz XTAL)
  202.             STAA    BAUD
  203.             LDAA    #$0C        ; Transmit & receive enable
  204.             STAA    SCCR2
  205.  
  206.             LDAA    #FLAGCHAR   ; Set up flag character in buffer
  207.             STAA    GLOVEFLAG
  208.  
  209.             LDAA    #CONTMODE   ; Start in continuous mode
  210.             STAA    MODEFLAG
  211.  
  212.             LDAA    #$FF        ; Start in deglitching mode
  213.             STAA    DEGLFLAG
  214.  
  215.             JSR     INITVAR     ; Initialize deglitching variables
  216.  
  217. INITGLOVE:  JSR     HIRES       ; Set hi-res mode
  218.  
  219. *
  220. * *********************
  221. * * Main program loop *
  222. * *********************
  223. *
  224.  
  225. MAIN:       LDAA    #0          ; Zero the retry counter
  226.             STAA    UNREADY
  227.             LDX     #D2SLOW     ; Wait a while
  228.             JSR     DELAY
  229.  
  230. CHECKRDY:   JSR     GETBYTE     ; Check to see if glove is ready
  231.             CMPA    #$A0        ; Is it A0 (the start of the sequence?)
  232.             BEQ     READDATA    ; Yes, read the rest of the data sequence
  233.  
  234.             INC     UNREADY     ; Increment retry counter
  235.             BEQ     INITGLOVE   ; If 256 tries, initialize the glove again
  236.  
  237.             LDX     #D2SLOW     ; Wait and try again
  238.             JSR     DELAY
  239.             BRA     CHECKRDY
  240.  
  241. READDATA:   LDY     #GLOVEDATA
  242.             JSR     GETGLOVE    ; Read glove data into buffer
  243.  
  244.             LDAA    DEGLFLAG    ; Check to see if we're supposed to deglitch
  245.             BEQ     CHECKSER    ; No - go on to keypresses
  246.  
  247.             LDY     #GLOVEDATA
  248.             JSR     DOHYST      ; *** Do hysterisis deglitch
  249.  
  250. CHECKSER:   LDAA    SCSR        ; Check serial port receive status
  251.             ANDA    #$20
  252.             BEQ     CHECKMODE
  253.  
  254.             LDAA    SCDR        ; Character received - check it
  255.  
  256.             CMPA    #QUERYCHAR  ; Is it the query character?
  257.             BNE     NOTQRYCH    ; No - skip this
  258.             LDAA    MODEFLAG    ; Yes - check to see what mode we're in
  259.             CMPA    #REQMODE    ; If not in request mode, skip this
  260.             BNE     CHECKMODE
  261.             LDY     #GLOVEDATA  ; Send 6 bytes over the serial port
  262.             LDAB    #6
  263.             JSR     SENDSER     ; This does our pausing for us (6.25 ms)
  264.             BRA     MAIN        ; No need to continue checking
  265.             
  266. NOTQRYCH:   CMPA    #CONTCHAR   ; Is it the cont. mode activator?
  267.             BNE     NOTCONTCH   ; No - skip this
  268.             LDAA    #CONTMODE   ; Yes - set continuous mode
  269.             STAA    MODEFLAG
  270.             BRA     CHECKMODE
  271.  
  272. NOTCONTCH:  CMPA    #REQCHAR    ; Is it the request mode activator?
  273.             BNE     NOTREQCH    ; No - skip this
  274.             LDAA    #REQMODE    ; Yes - set request mode
  275.             STAA    MODEFLAG
  276.             BRA     WAIT        ; No need to continue checking
  277.  
  278. NOTREQCH:   CMPA    #DEGLCHAR   ; Is it the deglitching on character?
  279.             BNE     NOTDEGCH    ; No - skip this
  280.             LDAA    #$FF        ; Yes - set the deglitching flag
  281.             STAA    DEGLFLAG
  282.             BRA     WAIT
  283.  
  284. NOTDEGCH:   CMPA    #NDGLCHAR   ; Is it the deglitching off character?
  285.             BNE     CHECKMODE   ; No - skip this
  286.             LDAA    #0          ; Yes - clear the deglitching flag
  287.             STAA    DEGLFLAG
  288.             BRA     WAIT
  289.  
  290. CHECKMODE:  LDAA    MODEFLAG    ; Check the mode flag
  291.             CMPA    #CONTMODE   ; Is it continuous mode?
  292.             BNE     WAIT        ; No - wait and start the loop again
  293.             LDY     #GLOVEFLAG  ; Send data (6 + Flag) over serial port
  294.             LDAB    #7
  295.             JSR     SENDSER     ; This provides approx. 7.29 ms of delay at 9600 baud
  296.             JMP     MAIN
  297.  
  298. WAIT:       LDX     #D2SLOW     ; Wait a while before continuing
  299.             JSR     DELAY
  300.             JMP     MAIN
  301.  
  302. *
  303. * **************************************
  304. * *          Delay subroutine          *
  305. * * (Delay proportional to value in X) *
  306. * **************************************
  307. *
  308.  
  309. DELAY:      DEX
  310.             BNE     DELAY
  311.             RTS
  312.  
  313. *
  314. * *******************
  315. * * Set hi-res mode *
  316. * *******************
  317. *
  318.  
  319. HIRES:      LDAB    #GCLOCK     ; Dummy read 4 dummy bits from glove
  320.             STAB    PORTA       ; Set Clock = 1, Latch = 0
  321.             LDAB    #GCLOLAT    ; Set Clock = 1, Latch = 1
  322.             STAB    PORTA
  323.             NOP                 ; Delay 3us
  324.             NOP
  325.             NOP
  326.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  327.             STAB    PORTA
  328.  
  329.             NOP                 ; Delay 3us
  330.             NOP
  331.             NOP
  332.             LDAB    #0          ; Set Clock = 0, Latch = 0
  333.             STAB    PORTA
  334.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  335.             STAB    PORTA
  336.             NOP                 ; Delay 3us
  337.             NOP
  338.             NOP
  339.             LDAB    #0          ; Set Clock = 0, Latch = 0
  340.             STAB    PORTA
  341.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  342.             STAB    PORTA
  343.             NOP                 ; Delay 3us
  344.             NOP
  345.             NOP
  346.             LDAB    #0          ; Set Clock = 0, Latch = 0
  347.             STAB    PORTA
  348.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  349.             STAB    PORTA
  350.             NOP                 ; Delay 3us
  351.             NOP
  352.             NOP
  353.             LDAB    #0          ; Set Clock = 0, Latch = 0
  354.             STAB    PORTA
  355.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  356.             STAB    PORTA
  357.  
  358.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  359.             STAB    PORTA
  360.             LDX     #2402       ; Delay 7212us
  361.             JSR     DELAY
  362.  
  363.             LDAB    #GCLOLAT    ; Set Clock = 1, Latch = 1
  364.             STAB    PORTA
  365.             LDX     #752        ; Delay 2260us
  366.             JSR     DELAY
  367.  
  368.             LDAA    #7
  369.             STAA    BYTECNT
  370.             LDY     #HRCODE     ; Send the 7 byte code
  371. BYTELOOP:   LDAA    #8
  372.             STAA    BITCNT
  373.             LDAA    0,Y
  374. BITLOOP:    LSLA
  375.             BCC     BITOFF
  376.             LDAB    #GCLOLAT    ; Set Clock = 1, Latch = 1
  377.             STAB    PORTA
  378.             LDAB    #GLATCH     ; Set Clock = 0, Latch = 1
  379.             STAB    PORTA
  380.             LDAB    #GCLOLAT    ; Set Clock = 1, Latch = 1
  381.             STAB    PORTA
  382.             BRA     NEXTLOOP
  383. BITOFF:     LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  384.             STAB    PORTA
  385.             LDAB    #0          ; Set Clock = 0, Latch = 0
  386.             STAB    PORTA
  387.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  388.             STAB    PORTA
  389. NEXTLOOP:   NOP                 ; Delay 3us
  390.             NOP
  391.             NOP
  392.             DEC     BITCNT
  393.             BNE     BITLOOP
  394.  
  395.             LDX     #D2BYTES
  396.             JSR     DELAY
  397.             INY
  398.             DEC     BYTECNT
  399.             BNE     BYTELOOP
  400.  
  401.             LDX     #296        ; Delay 892us
  402.             JSR     DELAY
  403.  
  404.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  405.             STAB    PORTA
  406.             LDX     #20000      ; Delay a "long time"
  407.             JSR     DELAY
  408.             RTS
  409.  
  410. *
  411. * Glove initialization bytes
  412. * Was HRCODE:     dc.b    $06,$C1,$08,$00,$02,$FF,$01
  413. *
  414.  
  415. HRCODE:     FCB     $06,$C1,$08,$00,$02,$FF,$01
  416.  
  417. *
  418. * *******************************************
  419. * *        Get the 6 byte data packet       *
  420. * * (Places data in buffer pointed to by Y) *
  421. * *******************************************
  422. *
  423.  
  424. GETGLOVE:   JSR     GETBYTE     ; Get each byte consecutively
  425.             STAA    0,Y         ; and store in memory
  426.             INY
  427.             LDX     #D2BYTES
  428.             JSR     DELAY
  429.  
  430.             JSR     GETBYTE
  431.             STAA    0,Y
  432.             INY
  433.             LDX     #D2BYTES
  434.             JSR     DELAY
  435.  
  436.             JSR     GETBYTE
  437.             STAA    0,Y
  438.             INY
  439.             LDX     #D2BYTES
  440.             JSR     DELAY
  441.  
  442.             JSR     GETBYTE
  443.             STAA    0,Y
  444.             INY
  445.             LDX     #D2BYTES
  446.             JSR     DELAY
  447.  
  448.             JSR     GETBYTE
  449.             STAA    0,Y
  450.             INY
  451.             LDX     #D2BYTES
  452.             JSR     DELAY
  453.  
  454.             JSR     GETBYTE
  455.             STAA    0,Y
  456.             LDX     #D2BYTES
  457.             JSR     DELAY
  458.  
  459.             JSR     GETBYTE     ; Throw away last two bytes
  460.             LDX     #D2BYTES
  461.             JSR     DELAY
  462.             JSR     GETBYTE
  463.             RTS
  464.  
  465. *
  466. * ************************
  467. * * Initialize variables *
  468. * ************************
  469. *
  470. INITVAR:    LDD     #0          ; Set all variables to 0
  471.             STD     OX          ; First hysterisis variables
  472.             STD     OY
  473.             RTS
  474.  
  475. *
  476. * *****************************
  477. * * Get a byte from the glove *
  478. * *    (Returns byte in A)    *
  479. * *****************************
  480. *
  481.  
  482. GETBYTE:    LDAB    #GCLOCK     ; Pulse the latch line
  483.             STAB    PORTA       ; Set Clock = 1, Latch = 0
  484.             LDAB    #GCLOLAT    ; Set Clock = 1, Latch = 1
  485.             STAB    PORTA
  486.             NOP                 ; Delay 3us
  487.             NOP
  488.             NOP
  489.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  490.             STAB    PORTA
  491.  
  492.             LDAA    #8
  493.             STAA    BITCNT
  494.  
  495. GETLOOP:    LSLA                ; Read 8 bits sequentially
  496.             LDAB    PORTA
  497.             ANDB    #GDATA      ; Mask off other bits
  498.             ABA                 ; Assemble the data byte
  499.             LDAB    #0          ; Set Clock = 0, Latch = 0
  500.             STAB    PORTA
  501.             LDAB    #GCLOCK     ; Set Clock = 1, Latch = 0
  502.             STAB    PORTA
  503.             DEC     BITCNT
  504.             BNE     GETLOOP
  505.  
  506.             RTS
  507.  
  508. *
  509. * *****************************
  510. * *    Hysterisis deglitch    *
  511. * * (Y points to data buffer) *
  512. * *****************************
  513. *
  514. DOHYST:     LDAA    5,Y         ; Check for center button
  515.             BNE     NOCENTER
  516.             JSR     INITVAR     ; If so, clear variables
  517.  
  518. NOCENTER:   CLRA
  519.             LDAB    0,Y         ; Begin hysterisis for X
  520.             BPL     NOXTNDHX    ; Sign extend B reg. into D
  521.             COMA
  522. NOXTNDHX:   STD     TMP
  523.             SUBD    OX
  524.             CPD     #XHYST
  525.             BLE     HYSTX1
  526.             LDD     TMP
  527.             SUBD    #XHYST
  528.             STD     OX
  529. HYSTX1:     LDD     OX
  530.             SUBD    TMP
  531.             CPD     #XHYST
  532.             BLE     HYSTY1
  533.             LDD     TMP
  534.             ADDD    #XHYST
  535.             STD     OX
  536.  
  537. HYSTY1:     CLRA
  538.             LDAB    1,Y         ; Begin hysterisis for Y
  539.             BPL     NOXTNDHY    ; Sign extend B reg. into D
  540.             COMA
  541. NOXTNDHY:   STD     TMP
  542.             SUBD    OY
  543.             CPD     #YHYST
  544.             BLE     HYSTY2
  545.             LDD     TMP
  546.             SUBD    #YHYST
  547.             STD     OY
  548. HYSTY2:     LDD     OY
  549.             SUBD    TMP
  550.             CPD     #YHYST
  551.             BLE     HYSTDONE
  552.             LDD     TMP
  553.             ADDD    #YHYST
  554.             STD     OY
  555.  
  556. HYSTDONE:   LDAA    OX+1        ; Put values back
  557.             STAA    0,Y
  558.             LDAA    OY+1
  559.             STAA    1,Y
  560.             RTS
  561.  
  562. *
  563. * ***********************************************
  564. * * Send an X byte packet over the serial port  *
  565. * * (Y contains the address of the data buffer, *
  566. * *  B contains the number of bytes to send)    *
  567. * ***********************************************
  568. *
  569.  
  570. SENDSER:    LDAA    SCSR        ; Check status register for Tx ready
  571.             ANDA    #$80
  572.             BEQ     SENDSER     ; Try again if not ready
  573.  
  574.             LDAA    0,Y         ; Send byte to the serial port
  575.             STAA    SCDR
  576.  
  577.             INY                 ; Move to next data byte
  578.             DECB
  579.             BNE     SENDSER     ; Send next byte if not done
  580.  
  581.             RTS
  582.  
  583. *
  584. * Uncomment these next lines for the homebrew board:
  585. *
  586. *    ORG     $FFFA               ; Reset vectors for standalone board
  587. *    FDB     $F800,$F800,$F800
  588.